<?php
/**
 *author:栗超
 *address:2788547340@qq.com
 * 2016/8/24 22:06
 */
 namespace Home\Controller;

use Common\Model\Cate;
use Common\Model\Goods;
use Common\Model\GoodsAttr;
use Common\Model\TypeAttr;
use Hdphp\Controller\Controller;

//前台控制器
class ListController extends Controller{
    public function index(){
        /**
         * 获取顶级分类
         */
        $cateModel=new Cate();
        $topCate=$cateModel->where("pid=0")->get();
//		p($topCate);
        /**
         * 通过当前分类的cid获得所有的子集的cid
         */
        $cid=Q('post.cid',0,'intval');
        $cids=$cateModel->getSon($cateModel->get(),$cid);
        $cids[]=$cid;
//		p($cids);

        /**
         * 根据cid获得商品的gid
         */
        $goodsModel=new Goods();
        $gids=$goodsModel->where("category_cid IN(".implode(',',$cids ).")")->lists('gid');
//		p($gids);
        if($gids){
            //4.通过gid到goods_attr表查询属性
            $goodsAttrModel=new GoodsAttr();
            $goodsAttr =$goodsAttrModel
                ->where("goods_gid IN (".implode(',',$gids ).")")
                ->groupBy('gavalue')
                ->get();

            //把相同taid的属性放在了一起
            $sameTemp=[];
            foreach ($goodsAttr as $v){
                $sameTemp[$v['type_attr_taid']][]=$v;
//                p($sameTemp);
            }
//            p($sameTemp);
            //组合名字和值
            $finalAttr=[];
            $typeAttrModel=new TypeAttr();
            foreach($sameTemp as $type_attr_taid=>$v){
                $finalAttr[]=[
                    'name'=>$typeAttrModel->where("taid=$type_attr_taid AND class=0 ")->pluck('taname'),
                    'value'=>$v,
                ];
            }
//            p($finalAttr);

            View::with('finalAttr',$finalAttr);
            /**
             * 筛选
             */
            //1.地址栏
            $num = count($finalAttr);
            $param = isset($_GET['param']) ? explode('_',$_GET['param']) : array_fill(0,$num,0);
            View::with("param",$param);
            //2.筛选操作,最终获得商品gid
//            p($param);
            $finalGids = $this->filter($param,$gids);
//            p($finalGids);
            if($finalGids){
                $data=$goodsModel->where("gid IN(".implode(',',$finalGids ).")")->get();
                View::with('data',$data);
            }else{
                View::error('没有该商品');
            }
        }else{
            View::with('finalAttr',[]);
        }
//        p($data);
        View::make();
    }

    /**
     * 获取交集
     */
    private function filter($param,$cidGids){
        $goodsAttrModel=new GoodsAttr();
        $gidArr=[];
        foreach ($param as $gaid){
            //点击的不是“全部”的时候
            //根据gaid获得gid
            if($gaid){
                //查询到商品的属性名称
                $gavalue=$goodsAttrModel->where("gaid={$gaid}")->pluck('gavalue');
                //通过名称查询到gid
                $gidArr[]=$goodsAttrModel->where(['gavalue'=>$gavalue])->lists('goods_gid');
            }
//           p($gidArr);
            //如果$gidArr有值的时候,也就是用户不是全部"不限"的时候
            if ($gidArr){
                $gid=$gidArr[0];
                foreach ($gidArr as $v){
                    $gid=array_intersect($v,$gid);
                }
//                p($gid);
                //因为$gid有可能是其他分类的gid，那么为了确保是当前分类，再次取交集
                $finalGids=array_intersect($gid,$cidGids );
            }else{
                $finalGids=$cidGids;
            }

        }
        return $finalGids;
    }


}
